<template>
  <div>
    <el-breadcrumb class="breadcrumb-row" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>广告管理</el-breadcrumb-item>
      <el-breadcrumb-item>广告列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row class="card-head-row">
        <el-col :span="6">
          <el-input v-model="search.title" placeholder="请输入搜索内容" size="small">
            <el-button slot="append" icon="el-icon-search" size="small" @click="searchForm"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="add">添加</el-button>
        </el-col>
      </el-row>

      <el-table border stripe :data="list.data" style="width: 100%" size="small">
        <el-table-column
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="title"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          prop="position.title"
          label="位置"
        >
        </el-table-column>
        <el-table-column
          label="图片"
        >
          <template slot-scope="scope">
            <el-image v-if="scope.row.image" style="width:50px;height:50px;" :src="scope.row.image" fit="fit"
                      :preview-src-list="[scope.row.image]"></el-image>
            <span v-else>#</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="url"
          label="URL"
        >
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
        >
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button circle icon="el-icon-edit" size="small" type="primary" @click="edit(scope.row)"></el-button>
            <el-button circle icon="el-icon-delete" size="small" type="danger" @click="del(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        layout="total, prev, pager, next" background
        :total="list.total"
        :page-size="list.pageSize"
        @current-change="pageChange">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {ad, status} from "../../request";

export default {
  name: "Index",
  data() {
    return {search: {}, list: {total: 0, pageSize: 15, data: []}};
  },
  created() {
    this.loadData();
  },
  methods: {
    /**
     * 加载数据
     */
    async loadData(params) {
      const res = await ad.list(params);
      if (res.code == status.SUCCESS) {
        this.list.total = res.data.total;
        this.list.data = res.data.data;
      }
    },
    /**
     * 搜索
     */
    searchForm() {
      this.loadData(this.search);
    },
    /**
     * 分页改变
     * @param page
     */
    pageChange(page) {
      this.loadData({page});
    },
    /**
     * 添加
     */
    add() {
      this.$router.push({name: 'AdAdd'});
    },
    /**
     * 编辑
     * @param row
     */
    edit(row) {
      this.$router.push({name: 'AdEdit', params: {id: row.id}});
    },
    /**
     * 删除
     * @param row
     */
    del(row) {
      this.$confirm('您确认删除吗？', '提示', {type: 'warning'}).then(async () => {
        const res = await ad.del({id: row.id});
        if (res.status == status.SUCCESS) {
          this.$message.success(res.message);
          this.loadData();
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
